package weibo4android.test;

import ian.android.lovewords.R;

import java.io.File;

import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class Main extends Activity {
	/** Called when the activity is first created. */
	String key = "", secret = "";
	Button login,send,showUser,logout;
	EditText status;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibo);
		
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",Weibo.CONSUMER_SECRET);

		login=(Button)findViewById(R.id.login);
		send=(Button)findViewById(R.id.send);
		showUser=(Button)findViewById(R.id.showUser);
		status=(EditText)findViewById(R.id.status);
		logout=(Button)findViewById(R.id.logout);
	
		login.setOnClickListener(new OnClickListener(){
 
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				login();
			}});
		
		send.setOnClickListener(new OnClickListener(){
 
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				send();
			}
		});
		
		showUser.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showUserInfo();
			}		
		});
		
		logout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				logout();
			}
			
		});
	}
 
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//鍚姩鏃舵墽琛屾娴嬫槸鍚︽潵鑷綉椤电櫥褰曡繑鍥�
		//濡傛灉鏄紝鑾峰彇key鍜宻ecret
		//鍚﹀垯璇诲彇SharedPreferences
		//鑻ュ緱涓嶅埌key鍜宻ecret锛岀洿鎺ヨ烦杞櫥褰�
		Uri uri = this.getIntent().getData();
		if (uri != null) {
			//濡傛灉鏄祻瑙堝櫒杩斿洖
			try {
				RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();
				AccessToken accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
				OAuthConstant.getInstance().setAccessToken(accessToken);
				// 淇濆瓨
				Editor sharedata = getSharedPreferences("WeiBo", MODE_PRIVATE).edit();
				sharedata.putString("key", accessToken.getToken());
				sharedata.putString("secret", accessToken.getTokenSecret());
				sharedata.commit();
				key = accessToken.getToken();
				secret = accessToken.getTokenSecret();
			} catch (WeiboException e) {
				e.printStackTrace();
			}
		} else {
			//濡傛灉鏄敤鎴疯嚜宸卞惎鍔�
			SharedPreferences settings = getSharedPreferences("WeiBo", MODE_PRIVATE);
			key = settings.getString("key", "");
			secret = settings.getString("secret", "");
		}
		if (key.equals("") || secret.equals("")) {
			Toast.makeText(this, "灏氭湭鐧诲綍", Toast.LENGTH_LONG).show();
			login();
			//璺宠浆鍒版祻瑙堝櫒鐧诲綍
 
		}
 
	}
	
	public void login(){
    	Weibo weibo = OAuthConstant.getInstance().getWeibo();
    	RequestToken requestToken;
		try {
			requestToken =weibo.getOAuthRequestToken("sina://weibo");
			Uri uri2 = Uri.parse(requestToken.getAuthenticationURL()+ "&from=xweibo");
			OAuthConstant.getInstance().setRequestToken(requestToken);
			startActivity(new Intent(Intent.ACTION_VIEW, uri2));
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}
	
	public void send(){
		String text=String.valueOf(status.getText());
		Weibo weibo =  new Weibo();
		weibo.setToken(key,secret);
		try {
			weibo.updateStatus(text);
			status.setText("鍙戣〃寰崥鎴愬姛!");
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void showUserInfo(){
		Weibo weibo = new Weibo();
		weibo.setToken(key,secret);
		try {
			User currentUser = weibo.verifyCredentials();
			String currentUID = String.valueOf(currentUser.getId());
			User user = weibo.showUser(currentUID);
			status.setText("screenName:"+user.getScreenName()+"\n"+
		               "statusText:"+user.getStatusText()+"\n"+
				       "location:"+user.getLocation()+"\n"+
		               "description:"+user.getDescription()+"\n"+
				       "followers:"+user.getFollowersCount()+"\n"+
		               "statusCount:"+user.getStatusesCount());
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void logout(){
		Weibo weibo = new Weibo();
		weibo.setToken(key, secret);
		try{
			User user = weibo.endSession();
			Editor sharedata = getSharedPreferences("WeiBo", MODE_PRIVATE).edit();
			sharedata.clear();
			sharedata.commit();
			login();
		} catch (WeiboException e){
			e.printStackTrace();
		}
	}
}
